package cn.crc.pjblog.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.Asset;
import org.apache.tapestry.ComponentResources;

import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.AssetSource;
import org.apache.tapestry.annotations.Path;

import cn.crc.model.BlogContent;

import cn.crc.model.imp.IPublicDAO;
import cn.crc.pjblog.data.LogWeatherDetail;
import cn.crc.pjblog.services.RedirectException;
import cn.crc.spring.SpringBean;

/**
 * @author doublel
 * 
 * 显示用户日志的详细内容字段，
 * 并且可以对该日志进行回复评论
 * */
public class Article {
	
	
	private String weatherPath="";
	@Inject
	@Path("context:images/Cprevious.gif")
	private Asset cprevious;
	
	@Inject
	@Path("context:images/Cnext.gif")
	private Asset cnext;
	
	@Inject
	@Path("context:images/rss.png")
	private Asset rss;

	@Inject
	@Path("context:images/icons/2.gif")
	private Asset icon2;
	
	@Inject
	@Path("context:images/From.gif")
	private Asset from;
	
	@Inject
	@Path("context:/images/icon_trackback.gif")
	private Asset trackback;
	
	@Inject
	@Path("context:/images/tag.gif")
	private Asset tag;
	
	
	@Inject
	@SpringBean("IPublicDAO")
	private IPublicDAO dao;
	
	
	@Inject 
	private ComponentResources resource;
	@Inject 
	private AssetSource assetSource;
	
	
	private BlogContent bc ;
    
	private String blogContent;
	
	private String id;
	
	LogWeatherDetail lw = LogWeatherDetail.getInstance();
	
	private Asset weatherImagePath;
	
	/**
	 * @param weatherImagePath the weatherImagePath to set
	 */
	public void setWeatherImagePath(Asset weatherImagePath) {
		this.weatherImagePath = weatherImagePath;
	}


	/**
	 * 获得页面传入的参数 id
	 * 然后对该记录的viewNums进行加一操作。统计浏览次数
	 * @author doublel
	 * @param String id 前一页传过来的参数
	 * */
	public void  onActivate(String id){
//		可以随意浏览BLog，这里不需要验证用户是否登录
//		if (!this.isLoginBlogMemberExists()){
//			
//			throw new RedirectException("exceptions/InvalidUserException");
//		}
		this.id = id;
		
		String hsql = "from BlogContent where Id="+id;

		List list = new ArrayList(0);
		try {
			list = this.dao.getDataByHSQL(hsql);
		} catch (Exception e) {
			// TODO: handle exception
			throw new RedirectException("exceptions/DbCRUDException");
			
		}
		System.out.println("list.size="+list.size());
		if (list.size()>=1) {

			this.bc = (BlogContent) list.get(0);
//			设置已经读入的次数加一
			
			if(bc.getLogViewnums() == null){
				bc.setLogViewnums(1);
			}else
			{
				bc.setLogViewnums(bc.getLogViewnums()+1);
			}

			try {
				this.dao.saveOrUpdate(bc);
			} catch (Exception e) {
				// TODO: handle exception
				throw new RedirectException("exceptions/DbCRUDException");
			}
			
		}else
		{
			bc = new BlogContent();
		}
		 
		if (lw.hnSunny.equals(bc.getLogWeather())){
			 this.weatherPath="hn2_sunny.gif";
			 
		 }else if (lw.hnIce.equals(bc.getLogWeather()))
		 {
			 this.weatherPath="hn2_ice.gif";
		 }else if (lw.hnCloudy.equals(bc.getLogWeather()))
		 {
			 this.weatherPath="hn2_cloudy.gif";
		 }
		
//		else
//		 {
//			this.weatherPath =  "hn2_sunny.gif";
//		 }
		System.out.println("bc.getLogWeather()="+bc.getLogWeather());
		System.out.println("this.weatherPath=="+this.weatherPath);
	}
	
	
	public String onPassivate(){
		return this.id;
		
	}
	
	public Asset getWeatherImagePath(){
	
		//this.resource.getBaseResource()
		return this.assetSource.findAsset(
				null, "context:images/weather/"+this.weatherPath, null);
	}
	
	
	/**
	 * @return the icon2
	 */
	public Asset getIcon2() {
		return icon2;
	}

	/**
	 * @param icon2 the icon2 to set
	 */
	public void setIcon2(Asset icon2) {
		this.icon2 = icon2;
	}

	/**
	 * @return the cnext
	 */
	public Asset getCnext() {
		return cnext;
	}

	/**
	 * @return the cprevious
	 */
	public Asset getCprevious() {
		return cprevious;
	}

	/**
	 * @return the rss
	 */
	public Asset getRss() {
		return rss;
	}

	/**
	 * @param cnext the cnext to set
	 */
	public void setCnext(Asset cnext) {
		this.cnext = cnext;
	}

	/**
	 * @param cprevious the cprevious to set
	 */
	public void setCprevious(Asset cprevious) {
		this.cprevious = cprevious;
	}

	/**
	 * @param rss the rss to set
	 */
	public void setRss(Asset rss) {
		this.rss = rss;
	}


	/**
	 * @return the dao
	 */
	public IPublicDAO getDao() {
		return dao;
	}


	/**
	 * @param dao the dao to set
	 */
	public void setDao(IPublicDAO dao) {
		this.dao = dao;
	}


	/**
	 * @return the bc
	 */
	public BlogContent getBc() {
		return bc;
	}


	/**
	 * @param bc the bc to set
	 */
	public void setBc(BlogContent bc) {
		this.bc = bc;
	}


	/**
	 * @return the from
	 */
	public Asset getFrom() {
		return from;
	}


	/**
	 * @return the tag
	 */
	public Asset getTag() {
		return tag;
	}


	/**
	 * @return the trackback
	 */
	public Asset getTrackback() {
		return trackback;
	}


	/**
	 * @param from the from to set
	 */
	public void setFrom(Asset from) {
		this.from = from;
	}


	/**
	 * @param tag the tag to set
	 */
	public void setTag(Asset tag) {
		this.tag = tag;
	}


	/**
	 * @param trackback the trackback to set
	 */
	public void setTrackback(Asset trackback) {
		this.trackback = trackback;
	}


	


	
		
}
